#导入tkinter
import calendar
from tkinter import *
#导入tkinter的meassagebox模块
from tkinter import messagebox
import time
from tkinter import simpledialog
from Main_leave import Main_leave
from Mian_history import HistoryWindow
from About import About
from User import User
from config.Instrument import Instrument_toml
from middleWare import Middleware
from advanced import SwitchDatabaseWindow
from intelligence.face import FaceRecognition as Face
from dao.Result_feature import Result as Res
from addface import FaceEntryApp
from intelligence.seconde import FaceRecognition as face_s
from photo import CameraApp

#以类的方式新建一个窗口
class Window():
    #启动函数，可以打开此窗口
    def __init__(self):
        tom = Instrument_toml()
        self.nickname = tom.GetStrogeToml("nickname")
        self.master = Tk()
        self.theme = 'dark'
        self.font_color = 'black' if self.theme == 'dark' else 'white'
        self.bg_color = '#fafafa' if self.theme == 'dark' else '#929497'
        self.navi_color = '#505458' if self.theme == 'light' else '#ffffff'
        #设置窗口图标，图标地址为static/favicon.ico
        self.master.iconbitmap('static/favicon.ico')
        #设置窗口标题
        self.master.title("学生管理系统")
        #设置背景为#fafafa
        self.master.configure(background=self.bg_color)
        self.width = self.master.winfo_screenwidth()  
        self.height = self.master.winfo_screenheight()  
  
        self.x = (self.width - 800) // 2  
        self.y = (self.height - 600) // 2   
  
        self.master.geometry("{}x{}+{}+{}".format(800, 600, self.x, self.y))  
        

        #TODO 导航栏

        #竖排导航栏
        self.text = Label(self.master, text=" ")
        self.text.configure(width=10,height=40,background=self.navi_color,fg=self.font_color)
        self.text.place(x=0, y=0)
        
        self.text_r = Label(self.master, text=" ")
        self.text_r.configure(width=40,height=40,background=self.navi_color,fg=self.font_color)
        self.text_r.place(x=580, y=0)
        
        #显示一张图片，该图片可以点击
        self.img = PhotoImage(file='static/考勤(未选中).png')
        #创建按钮并将图片设置为按钮的图片
        self.btn = Button(self.master,image=self.img, width=40, height=40,relief=FLAT,fg=self.font_color,padx=3,pady=3,background=self.navi_color,command=lambda: self.click("考勤"))
        self.btn.place(x=15, y=100)

        self.img1 = PhotoImage(file='static/历史行程.png')
        #创建按钮并将图片设置为按钮的图片
        self.btn1 = Button(self.master,image=self.img1, width=40, height=40,relief=FLAT,fg=self.font_color,padx=3,pady=3,background=self.navi_color,command=lambda: self.click("历史行程"))
        self.btn1.place(x=15, y=190)

        self.img2 = PhotoImage(file='static/请假.png')
        #创建按钮并将图片设置为按钮的图片
        self.btn2 = Button(self.master,image=self.img2, width=40, height=40,relief=FLAT,fg=self.font_color,padx=3,pady=3,background=self.navi_color,command=lambda: self.click("请假"))
        self.btn2.place(x=15, y=270)
    
        self.img3 = PhotoImage(file='static/设置.png')
        #创建按钮并将图片设置为按钮的图片
        self.btn3 = Button(self.master,image=self.img3, width=40, height=40,relief=FLAT,fg=self.font_color,padx=3,pady=3,background=self.navi_color,command=lambda: self.click("设置"))
        self.btn3.place(x=15, y=450)

        self.img4 = PhotoImage(file='static/退出.png')
        #创建按钮并将图片设置为按钮的图片
        self.btn4 = Button(self.master,image=self.img4, width=40, height=40,relief=FLAT,fg=self.font_color,padx=3,pady=3,background=self.navi_color,command=self.back)
        self.btn4.place(x=10, y=530)

        #TODO 主页面
        #创建一个文本域，文本内容为：欢迎您！
        self.main_text_1 = Label(self.master, text="欢迎您！",font=("微软雅黑", 20))
        #背景色为#fafafa
        self.main_text_1 .configure(background=self.bg_color)
        self.main_text_1.place(x=100, y=50)

        #设置一个时钟
        self.clock = Label(self.master, font=("微软雅黑", 10),bg=self.navi_color,fg='#3b80f9')
        self.clock.place(x=700, y=40)

        #设置一个文本域，用于显示年月日星期几
        self.main_text_2 = Label(self.master,font=("微软雅黑", 10),background='#3b80f9',fg=self.font_color)
        #背景色为#fafafa
        self.main_text_2.configure(background=self.bg_color)
        self.main_text_2.place(x=600, y=60)

        #设置一个文本域，文本为考勤
        self.main_text_3 = Label(self.master, text="考勤类型",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.main_text_3.configure(background=self.bg_color)
        self.main_text_3.place(x=150, y=120)

        self.main_text_4 = Label(self.master, text="默认",font=("微软雅黑", 10),fg='#3b80f9')
        #设置背景色为#fafafa
        self.main_text_4.configure(background=self.bg_color)
        self.main_text_4.place(x=300, y=125)

        #设置一个文本域 文本为班级 
        self.main_text_5 = Label(self.master, text="班       级",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.main_text_5.configure(background=self.bg_color)
        self.main_text_5.place(x=150, y=170)

        #设置一个输入框，边框色为#3b80f9
        self.main_text_6 = Entry(self.master, text="请输入班级",font=("微软雅黑", 10),background=self.navi_color)
        #设置默认输入值为：请输入班级
        self.main_text_6.insert(0, "请输入班级")
        self.main_text_6.place(x=300, y=175)
        
        #设置一个文本域，文本内容为教师
        self.main_text_7 = Label(self.master, text="教       师",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.main_text_7.configure(background=self.bg_color)
        self.main_text_7.place(x=150, y=220)

        #设置一个输入框，默认输入为教师工号
        self.main_text_8 = Label(self.master, text=self.nickname,font=("微软雅黑", 10),background=self.navi_color,fg=self.font_color)
        self.main_text_8.place(x=300, y=225)

        #设置一个文本域，文本内容为时间
        self.main_text_9 = Label(self.master, text="时       间",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.main_text_9.configure(background=self.bg_color)
        self.main_text_9.place(x=150, y=270)
        #获取当前日期
        self.date = "2023-10-01"
        
        #设置一个区域用于显示此时间
        self.main_text_10 = Label(self.master, text=self.date,font=("微软雅黑", 10),bg=self.navi_color,fg='#3b80f9')
        #设置背景色为#fafafa
        self.main_text_10.configure(background=self.bg_color)
        self.main_text_10.place(x=300, y=275)

        #设置一个按钮用于重新获取时间
        self.main_btn_1 = Button(self.master, text="重新获取",font=("微软雅黑", 10),background="#3b80f9",relief='flat',fg='white',padx=1,pady=1,command=self.get_time)
        self.main_btn_1.place(x=390, y=270)

        #清除班级
        def clear():
            self.show_crame()

        #设置一个按钮，文本为清除
        self.main_btn_2 = Button(self.master, text="拍照",font=("微软雅黑", 10),background="#3b80f9",relief='flat',fg='white',padx=1,pady=1)
        #配置宽高
        self.main_btn_2.configure(width=10, height=1,command=clear)
        self.main_btn_2.place(x=410, y=360)

        #设置一个按钮，文本为确认
        self.main_btn_3 = Button(self.master, text="请假情况",font=("微软雅黑", 10),background="#3b80f9",relief='flat',fg='white',padx=1,pady=1)
        #配置宽高
        self.main_btn_3.configure(width=10, height=1,command=self.tohis)
        self.main_btn_3.place(x=410, y=410)

        #使用tkinter画一个圆
        self.circle = Canvas(self.master, width=110, height=110, bg='#3b80f9', bd=0, highlightthickness=0)
        self.circle.place(x=200, y=350)
        self.circle.create_oval(0, 0, 100, 100, fill='#3b80f9')
        #设置没有背景色
        self.circle.configure(background=self.bg_color)
        #设置圆范围内为白色
        self.circle.create_oval(0, 0, 100, 100, fill='#6299fa',outline='#3b80f9')

        #设置图片为按钮
        self.mian_img = PhotoImage(file='static/考勤_提交.png')
        self.main_btn_4 = Button(self.master, image=self.mian_img, width=50, height=50,bg='#6299fa',relief='flat')
        self.main_btn_4.place(x=223, y=360)
        self.main_btn_4.config(command=self.revise)

        #设置一个文本域，内容为发起考勤
        self.mian_text_11 = Label(self.master, text="发起考勤",bg='#6299fa',fg=self.navi_color)
        self.mian_text_11.place(x=223, y=410)
        
        #设置一个区域来显示图片
        self.image_1 = PhotoImage(file="static/汉字(卡).png")
        self.mian_image_1 = Label(self.master, image=self.image_1,bg=self.bg_color)
        self.mian_image_1.place(x=123, y=500)
        
        #设置一个文本域，文本内容为:课程打卡
        self.mian_text_12 = Label(self.master, text="课程打卡 上课时间(8:30-12:10)",bg=self.bg_color)
        self.mian_text_12.place(x=150, y=503)
        self.mian_text_12.config(font=("宋体", 10))

        #设置一个区域来显示新的图片
        self.image_2 = PhotoImage(file="static/勾.png")
        self.mian_image_2 = Label(self.master, image=self.image_2,background=self.bg_color)
        self.mian_image_2.place(x=123, y=535)

        #设置一个文本区域来显示文本
        self.mian_text_13 = Label(self.master, text="正常使用",bg=self.bg_color)
        self.mian_text_13.place(x=150, y=537)
        self.mian_text_13.config(font=('宋体', 10))

        

        # TODO 公共工具栏部分

        self.get_time()
        #设置一个区域，制作一个日历
        datelis = self.date.split('-')
        year = int(datelis[0])
        month = int(datelis[1])
        day = int(datelis[-1])
        cal = calendar.monthcalendar(year, month)
        for i in range(len(cal)):
            for j in range(len(cal[i])):
                if cal[i][j] != 0:
                    f_c = 'black' if cal[i][j] != day else '#ffffff'
                    b_c = 'white' if cal[i][j] != day else '#3b80f9'
                    calendar_label = Label(self.master, text=cal[i][j], width=1, height=1,background=b_c,fg=f_c)
                    calendar_label.place(x=j*25+600, y=i*30+160)

        for i in range(7):
            dic={
                0:"一",
                1:"二",
                2:"三",
                3:"四",
                4:"五",
                5:"六",
                6:"日"
            }
            font_color = "#3b80f9" if i != 6 else "white"
            back_color = "#ffffff" if i != 6 else "#3b80f9"
            self.main_text_11 = Label(self.master, text=dic[i],font=("微软雅黑", 10),fg=font_color)
            #设置背景色为#3b80f9
            self.main_text_11.configure(background=back_color)
            self.main_text_11.place(x=i*24+600, y=120)

        #读取本地头像文件以展示
        self.image_3 = PhotoImage(file="static/用户头像.png")
        # self.public_image_3 = Label(self.master, image=self.image_3,background='#ffffff')
        # self.public_image_3.place(x=600, y=500)
        # #设置一个按钮
        self.public_btn_1 = Button(self.master, text="退出",font=("微软雅黑", 10),background="#3b80f9",relief='flat',fg='white',padx=1,pady=1)
        self.public_btn_1.configure(width=20, height=20,image=self.image_3,background="#ffffff",activebackground="#ffffff",command=self.info)
        self.public_btn_1.place(x=600, y=500)

        #设置文本域展示用户
        self.public_text_1 = Label(self.master, text=self.nickname,font=("微软雅黑", 10),bg='#ffffff')
        self.public_text_1.place(x=640, y=500)
        
        #设置文本域展示文本，
        self.public_text_2 = Label(self.master, text="我想不上班也有钱！",font=("微软雅黑", 10),bg='#ffffff')
        self.public_text_2.place(x=600, y=530)

        self.update_desc()

        #TODO 设置页面

        #创建一个文本域，文本内容为：设置
        self.text1 = Label(self.master, text="设置",font=("微软雅黑", 20))
        #设置背景色为#fafafa
        self.text1.configure(background="#fafafa")

        self.text2 = Label(self.master, text="——",font=("微软雅黑", 10))
        #设置背景色为#fafafa
        self.text2.configure(background="#fafafa")


        #创建一个文本域，文本为：切换模式，背景色为#fafafa
        self.text3 = Label(self.master, text="高级",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.text3.configure(background="#fafafa")

        #创建一个按钮，文本为：切换 背景色为#3b80f9
        self.btn5 = Button(self.master, text=">",font=("微软雅黑", 10),background="#fafafa",relief='flat',fg='#3b80f9',padx=5,pady=5)
        self.btn5.configure(command=self.advanced)
        
        #创建一个文本域，文本为：版本信息，背景色为#fafafa
        self.text4 = Label(self.master, text="添加班级",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.text4.configure(background="#fafafa")

        #创建一个按钮，文本为: > 背景色为#3b80f9
        self.btn6 = Button(self.master, text=">",font=("微软雅黑", 10),background="#fafafa",relief='flat',fg='#3b80f9',padx=5,pady=5)
        self.btn6.config(command=self.addface)

        self.text5 = Label(self.master, text="关于我",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.text5.configure(background="#fafafa")

        #创建一个按钮，文本为: > 背景色为#3b80f9
        self.btn7 = Button(self.master, text=">",font=("微软雅黑", 10),background="#fafafa",relief='flat',fg='#3b80f9',padx=5,pady=5,command=self.about)   

        self.text7 = Label(self.master, text="联系我吧",font=("微软雅黑", 15))
        #设置背景色为#fafafa
        self.text7.configure(background="#fafafa")

        #创建一个按钮，文本为: > 背景色为#3b80f9
        self.btn8 = Button(self.master, text=">",font=("微软雅黑", 10),background="#fafafa",relief='flat',fg='#3b80f9',padx=5,pady=5,command=self.contact)  
        

        #TODO 请假详情
        self.leave = Main_leave(self.master)
        self.leave.windw()

        #TODO 历史记录
        #在函数中

        #不断刷新使时间变化
        self.update_time()
        self.update_yar()

    def addface(self):
        window = Toplevel(self.master)
        app  = FaceEntryApp(window)
        app.run()



    #将考勤结果加入数据库并且显示就完成了

    #识别按钮
    def revise(self):
        mid = Middleware()
        dic = mid.get_config()
        res = Res(**dic)
        result = simpledialog.askstring("", "请输入线路(1,2)")
        if result == "1":
            face = Face(target_name_count=3, timeout_seconds=30)
            face.add_face(res)
            class_info = self.main_text_6.get()
            if class_info != "请输入班级":
                ans = face.run()
                tom = Instrument_toml()
                id = mid.data_binding_user_by_id(tom.GetStrogeToml('nickname'))['user_id']
                data = {
                'user_id' : id,
                'class'   : class_info,
                'lis'     : ans,
                }
                answer = mid.calculate(**data)
                messagebox.showinfo("成功","本次考勤共{x}人".format(x=len(ans)))
            else:
                messagebox.showwarning("警告","请输入班级")
        else:
            face = face_s()
            face.add_face(res)
            class_info = self.main_text_6.get()
            if class_info != "请输入班级":
                ans = face.recognize_faces("static/temp/temp.png")
                tom = Instrument_toml()
                id = mid.data_binding_user_by_id(tom.GetStrogeToml('nickname'))['user_id']
                data = {
                'user_id' : id,
                'class'   : class_info,
                'lis'     : ans,
                }
                answer = mid.calculate(**data)
                messagebox.showinfo("成功","本次考勤共{x}人".format(x=len(ans)))
            else:
                messagebox.showwarning("警告","请输入班级")

    def show_crame(self):
        window  = Toplevel(self.master)
        app = CameraApp(window,"test",0)
        app.start()

    def advanced(self):
        #弹出确认框
        if messagebox.askokcancel("提示", "高级设置需要重启，是否继续?"):
            self.master.destroy()
            switch_db = SwitchDatabaseWindow()  
            switch_db.run()
        else:
            pass

    
    def info(self):
        
        Window = Toplevel(self.master)
        app = User(Window)
        app.start()


    def about(self):
        Window = Toplevel(self.master)
        app = About(Window)
        app.window()


    def contact(self):
        #点击后复制信息到剪切板
        text = "QQ:123456789\n微信:123456789\n邮箱:123456789@qq.com"
        self.master.clipboard_append(text)

        messagebox.showinfo("提示", "已复制到剪切板")

    def tohis(self):
        self.hidden_module_attendance()
        self.leave.un_hidden_windw()


    def get_time(self):
        #获取当前日期
        self.date = time.strftime("%Y-%m-%d")
        self.main_text_10.config(text=self.date)
    def update_desc(self):
        mid = Middleware()
        ans = mid.data_binding_user_by_id(self.nickname)
        desc = ans['user_desc']
        self.public_text_2.config(text=desc)
        self.public_text_2.after(1000, self.update_desc)

    def update_time(self):
        #获取当前时间
        time_str = time.strftime("%H:%M:%S", time.localtime())
        #设置时钟显示的内容
        self.clock.config(text=time_str)
        #设置时钟的刷新频率
        self.clock.after(1000, self.update_time)

    def update_yar(self):
         #获取当前时间，年月日周几
        time_str = time.strftime("%Y年%m月%d日 %A", time.localtime())
        #设置时钟显示的内容
        self.main_text_2.config(text=time_str)
        #设置时钟的刷新频率
        self.main_text_2.after(1000, self.update_yar)


    # def change_color(self):
    #     # #929497 导航栏颜色
    #     # #505458  主题颜色

    #隐藏考勤模块
    def hidden_module_attendance(self):
        self.main_text_1.place_forget()
        self.mian_text_13.place_forget()
        self.mian_image_2.place_forget()
        self.mian_text_12.place_forget()
        self.mian_image_1.place_forget()
        self.mian_text_11.place_forget()
        self.main_btn_4.place_forget()
        self.circle.place_forget()
        self.main_btn_3.place_forget()
        self.main_btn_2.place_forget()
        self.main_btn_1.place_forget()
        self.main_text_10.place_forget()
        self.main_text_9.place_forget()
        self.main_text_8.place_forget()
        self.main_text_7.place_forget()
        self.main_text_6.place_forget()
        self.main_text_5.place_forget()
        self.main_text_4.place_forget()
        self.main_text_3.place_forget()
        # self.main_text_2.place_forget()
    
    #显示考勤模块
    def un_hidden_module_attendance(self):
        self.main_text_1.place(x=100, y=50)
        self.mian_text_12.place(x=150, y=503)
        self.mian_text_13.place(x=150, y=537)
        self.mian_image_2.place(x=123, y=535)
        self.mian_image_1.place(x=123, y=500)
        self.mian_text_11.place(x=223, y=410)
        self.main_btn_4.place(x=223, y=360)
        self.main_btn_1.place(x=390, y=270)
        self.circle.place(x=200, y=350)
        self.main_btn_3.place(x=410, y=410)
        self.main_btn_2.place(x=410, y=360)
        self.main_btn_2.place(x=410, y=360)
        self.main_text_10.place(x=300, y=275)
        self.main_text_9.place(x=150, y=270)
        # self.main_text_9.place(x=150, y=270)
        self.main_text_8.place(x=300, y=225)
        self.main_text_7.place(x=150, y=220)
        self.main_text_6.place(x=300, y=175)
        # self.main_text_6.place(x=300, y=175)
        self.main_text_5.place(x=150, y=170)
        self.main_text_4.place(x=300, y=125)
        self.main_text_3.place(x=150, y=120)


    def hidden_module_setting(self):
        #隐藏text组件
        self.text1.place_forget()
        self.text2.place_forget()
        self.text3.place_forget()
        self.text4.place_forget()
        self.text5.place_forget()
        self.text7.place_forget()
        self.btn5.place_forget()
        self.btn6.place_forget()
        self.btn7.place_forget()
        self.btn8.place_forget()

    def un_hidden_module_setting(self):
        #显示text组件
        self.text1.place(x=140,y=80)
        self.text2.place(x=140, y=115)
        self.text3.place(x=140, y=180)
        self.text4.place(x=140, y=250)
        self.text5.place(x=140, y=330)
        self.text7.place(x=140, y=400)
        self.btn5.place(x=520, y=170)
        self.btn6.place(x=520, y=240)
        self.btn7.place(x=520, y=320)
        self.btn8.place(x=520, y=390)

    def start(self):
        self.master.mainloop()
    
    def click(self,name):
        if name == "考勤":
            self.img = PhotoImage(file='static/'+name+'(未选中).png')
            self.btn.configure(image=self.img)
            self.img1 = PhotoImage(file='static/历史行程.png')
            self.btn1.configure(image=self.img1)
            self.img2 = PhotoImage(file='static/请假.png')
            self.btn2.configure(image=self.img2)
            self.img3 = PhotoImage(file='static/设置.png')
            self.btn3.configure(image=self.img3)
            #隐藏的组件
            self.hidden_module_setting()
            self.un_hidden_module_attendance()
            self.leave.hidden_windw()
            self.history.hidden_window()
            '''
                这里还有好东西
            '''



        if name == "历史行程":
            self.img = PhotoImage(file='static/考勤.png')
            self.btn.configure(image=self.img)
            self.img1 = PhotoImage(file='static/'+name+'(未选中).png')
            self.btn1.configure(image=self.img1)
            self.img2 = PhotoImage(file='static/请假.png')
            self.btn2.configure(image=self.img2)
            self.img3 = PhotoImage(file='static/设置.png')
            self.btn3.configure(image=self.img3)

            self.hidden_module_setting()
            self.hidden_module_attendance()
            self.leave.hidden_windw()
            
            self.history = HistoryWindow(self.master)
            self.history.window()
            self.history.hidden_r()

            self.history.un_hidden_window()
            self.history.btn.config(image=self.history.photo)
            self.history.text2.config(fg="#3b80f9")
            self.history.btn2.config(image=self.history.photo2)
            self.history.text3.config(fg="#929497")
            
            '''
                这里还有好东西
            '''

        if name == "请假":
            self.img = PhotoImage(file='static/考勤.png')
            self.btn.configure(image=self.img)
            self.img1 = PhotoImage(file='static/历史行程.png')
            self.btn1.configure(image=self.img1)
            self.img2 = PhotoImage(file='static/'+name+'(未选中).png')
            self.btn2.configure(image=self.img2)
            self.img3 = PhotoImage(file='static/设置.png')
            self.btn3.configure(image=self.img3)
            self.leave.text7.config(text=self.nickname)
            self.hidden_module_setting()
            self.hidden_module_attendance()
            self.leave.un_hidden_windw()
            self.history.hidden_window()
            self.leave.Defult()
            '''
                这里还有好东西
            '''



        if name == "设置":
            self.img = PhotoImage(file='static/考勤.png')
            self.btn.configure(image=self.img)
            self.img1 = PhotoImage(file='static/历史行程.png')
            self.btn1.configure(image=self.img1)
            self.img2 = PhotoImage(file='static/请假.png')
            self.btn2.configure(image=self.img2)
            self.img3 = PhotoImage(file='static/'+name+'(未选中).png')
            self.btn3.configure(image=self.img3)
            self.un_hidden_module_setting()
            self.hidden_module_attendance()
            self.leave.hidden_windw()
            self.history.hidden_window()
            '''
            
                    这里还有好东西
            '''

    def back(self):
        self.master.destroy()


#启动此类
# if __name__ == '__main__':
#     app = Window()
#     app.start()
